#ifndef TSFUN_H_
#define TSFUN_H_
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/*===========================================================
*ts time 
*
**/

#if defined(_WIN32) && !defined(__MINGW32CE__)
#include <windows.h>
#define msleep                  Sleep
#define sleep(x)                Sleep((x)*1000)
#else
#include <unistd.h>
#define msleep(x)               usleep((x)*1000)
#endif
/**
*
*
*===========================================================*/


/*===========================================================
*ts log
*
**/
static inline int prtgrade()
{
	static char *grade_s = NULL;
	if(grade_s == NULL)
	{
		grade_s = getenv("PRTOVERLAY");
		if(grade_s == NULL)
			return 0;
	}

	return atoi(grade_s);
}

#define TSPRTERROR(...) \
        do  \
        {   \
            if(prtgrade() > 0)  \
            {   \
                printf("tsError %s:%d>\t",__func__,__LINE__); \
                printf(__VA_ARGS__); \
            }   \
        } while(0)

#define TSPRTWARN(...) \
        do  \
        {   \
            if(prtgrade() > 1)  \
            {   \
                printf("tsWarn %s:%d>\t",__func__,__LINE__); \
                printf(__VA_ARGS__); \
            }   \
        } while(0)
#define TSPRTINFO(...) \
        do  \
        {   \
            if(prtgrade() > 2)  \
            {   \
                printf("tsInfo %s:%d>\t",__func__,__LINE__); \
                printf(__VA_ARGS__); \
            }   \
        } while(0)

#define TSPRTDEBUG(...) \
					do	\
					{	\
						if(prtgrade() > 3)	\
						{	\
							printf("tsDebug %s:%d>\t",__func__,__LINE__); \
							printf(__VA_ARGS__); \
						}	\
					} while(0)
			

/**
*
*
*===========================================================*/





/*===========================================================
*get linux time fun
*
**/
static inline unsigned int tf_getcurtime(int type)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    if(type == 0)
        return (unsigned int) (tv.tv_sec * 1000 + tv.tv_usec / 1000 ); // microseconds to millis
    else
        return (unsigned int) (tv.tv_sec * 1000000 + tv.tv_usec );
}
/**
*
*
*===========================================================*/


/*=========================================================
*fps printf count
*
*/
static int tf_framenumtime = 1;
volatile static int tf_Jpg_number = 0;
static inline void *tf_pthread_count(void *caller)
{

                while(1)
                {
                        sleep(tf_framenumtime);
                        TSPRTDEBUG("caller %s fps:%d\n",(char *)caller,tf_Jpg_number/tf_framenumtime);
                        tf_Jpg_number = 0;
                }
				return NULL;
}

static inline void tf_frame_display()
{
	static int tf_frame_display_ret = 0;
	if(tf_frame_display_ret == 0)
	{
		tf_frame_display_ret = 1;
		pthread_t pid = 0;
		pthread_create(&pid , NULL , tf_pthread_count ,(void *)__func__);
	}
	tf_Jpg_number++;
}
/**
*
*
*=========================================================*/





#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
static int fb_fd = -1 ;
static char * g_curfbmem = NULL;
static struct fb_var_screeninfo g_fb_var;
static int fb_width = 0;
static int fb_height = 0;
static inline int mmapfb0()
{

    if(g_curfbmem == NULL || fb_fd == -1)
    {
        fb_fd = open("/dev/fb0" , O_RDWR);
        if(fb_fd < 0)
        {
            perror("open fb0 error:");
            return -1;
        }

        if(ioctl(fb_fd , FBIOGET_VSCREENINFO, &g_fb_var) == -1)
        {
            perror("FBIOGET_VSCREENINFO error:");
            return -1;
        }


        g_curfbmem = (char *)mmap(0, g_fb_var.xres * g_fb_var.yres *g_fb_var.bits_per_pixel / 8 * 2  , PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
		
		fb_width = g_fb_var.xres;
		fb_height = g_fb_var.yres;
		memset(g_curfbmem , 0 , 4*fb_height*fb_width);

	}



    return 1;

}



static inline int read_frame_from_path(char* path, char *suffix ,unsigned char** buf)
{
	static int g_file_index = 0;

	
	char file_str[100];
	memset(file_str, 0, 100);
	sprintf(file_str, "%s/%d.%s", path , g_file_index,suffix);
	g_file_index++;

    FILE *fp;
    unsigned int filesize;
	//printf("file_str = %s\n",file_str);
    fp = fopen(file_str, "rb");
    if (!fp) {
            g_file_index = 0;
            return 0;
    }

	
    fseek(fp, 0, SEEK_END);
    filesize = ftell(fp);
	*buf = ( unsigned char*)malloc(filesize);
	fseek(fp, 0, SEEK_SET);
    if(fread(*buf, 1, filesize, fp)!=filesize)
    {
		free(buf);
		fclose(fp);
		return 0;
	}
    fclose(fp);
    return filesize;
}



static inline int write_data_to_file(char * path , char *data , int size)
{
	FILE *fp = NULL;
	fp = fopen(path , "wb");
	if(fp)
	{
		fwrite(data , 1 , size , fp);
		fclose(fp);
	}
	return 0;
}

#endif//TSFUN_H_
